# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
########################################
# 
# 功能：对指定文件夹里的png、jpg进行加密处理
# 
# 
########################################
import os
import sys
import random
import csv 
import operator 
import glob
import xxtea



#images_path是需要加密的图片的绝对路径
def encrypt(images_path,strTeakey,strTeasign):
    f = open(images_path, 'rb')
    filedata = f.read()
    filesize = f.tell()
    f.close()
    # num = 74
    # strTeakey = "xx"+ str(num) #加密密码
    # strTeasign = "xxsin"+ str(num)  
    #print(filedata[0:len(strTeasign)],"_______________")
    #print(strTeasign)
    if filedata[0:len(strTeasign)] == strTeasign:       
        filedata = filedata[len(strTeasign):len(filedata)]
        #解密
        #decrypt(images_path,filedata,strTeakey)
    else:
        s = xxtea.encrypt(filedata, strTeakey)
        f2 = open(images_path,'wb')
        f2.write(strTeasign+s)
        f2.close()        


#解密
def decrypt(images_path,filedata,key):
    filedata = xxtea.decrypt(filedata,key)
    f2 = open(images_path,'wb')
    f2.write(filedata)
    f2.close()    

#判断是否是png和jpg图片
def judge_is_png_or_jpg(file_name):
    suffix = os.path.splitext(file_name)[1]
    if  suffix== '.png' or suffix == '.jpg' or suffix == '.PNG' or suffix == '.JPG':
        return True
    else:
        return False

#输出加密图片
def saveFile(filePath,fileData):
    f2 = open(filePath,'wb')
    f2.write(fileData)
    f2.close()    

def encryptImgFile(path,key,sign):
    for root , dirs, files in os.walk(path):
        for name in files:                        
            #if name.endswith(".png") or name.endswith(".jpg") or name.endswith(".PNG") or name.endswith(".JPG"):
            if  not name.endswith(".mp3"):#zip 音乐不加密
                nname = sign+name
                os.rename(root+"/"+name,root+"/"+nname)
                filePath = os.path.join(root,nname)
                encrypt(filePath,key,sign)
                #print("encrypt File: " ,root,name, filePath)

# if __name__ == "__main__":
#     curPath = os.getcwd()
#     upPath = os.path.dirname(os.getcwd())#取当前目录上一级目录
#     #curPath = raw_input('Please enter the filename:').strip()
#     print("...... start.")
#     encryptImgFile(upPath+"/res")
#     print("...... end.")
